package com.itcs.se.security.resolver;

import com.alibaba.fastjson.JSON;
import com.itcs.box.domain.BoxRequest;
import com.itcs.box.server.resolver.MsgResolverAdapter;
import com.itcs.box.server.resolver.Send2AppThread;
import com.itcs.box.server.server.BoxToSeService;
import com.itcs.box.util.LogUtils;
import com.itcs.eureka.service.PayApiFeignClientService;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName: Box1022Resolver
 * @Description: SE发行请求，同步返回SE发行响应
 * @author: CHEN-HF
 * @date: 2018年10月20日 上午11:59:34
 * 
 * @Copyright: 2018 Inc. All rights reserved. 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目
 */
@Component
public class Box1024Resolver extends BoxToSeService implements MsgResolverAdapter {

	private ExecutorService executorService = Executors.newFixedThreadPool(10);

	private Log logger = LogFactory.getLog(getClass());

	@Resource(name="payApiRest")
	private PayApiFeignClientService apiService;

	/**安全校验模式*/
	@Value("${itcs.security.mode:A}")
	private String securityMode;

	@Override
	public boolean support(BoxRequest boxRequest) {
		return "1024".equals(boxRequest.getMessgeType());
	}

	@Override
	public void resolve(final ChannelHandlerContext channelHandlerContext, BoxRequest boxRequest) {

		String devId = boxRequest.getData().getDeviceId();
		String transNo = boxRequest.getData().getTransacSeq();
		logger.info(LogUtils.build("1022", "模式" + securityMode + "SE发行请求2",
				"通讯盒:" + devId + ",流水号:" + transNo, JSON.toJSONString(boxRequest)));
		boxRequest.setMessgeType(securityMode + "1024");
		Send2AppThread appThread = new Send2AppThread(apiService, serverId, channelHandlerContext, boxRequest);
		executorService.execute(appThread);
	}

}
